package net.adetech.police.api.impl;

import com.dragonsoft.node.adapter.comm.RbspCall;
import com.dragonsoft.node.adapter.comm.RbspConsts;
import com.dragonsoft.node.adapter.comm.RbspService;
import net.adetech.police.api.IPoliceInterfaceService;
import net.adetech.police.entity.People;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PoliceInterfaceServiceImpl implements IPoliceInterfaceService {

    @Value("${interface.url}")
    private String url;
    @Value("${interface.userCardId}")
    private String userCardId;
    @Value("${interface.userDept}")
    private String userDept;
    @Value("${interface.userName}")
    private String userName;


    @Override
    public People getPeopleBySfzh(String sfzh) {
        String condition = " sfzh = '" + sfzh + "' "; //查询条件
        Map<String, Object> params = new HashMap<>();

        params.put("DataObjectCode", "A001"); //数据集名称
        params.put("InfoCodeMode", "1");
        params.put("Condition", condition);
        params.put("RequiredItems", new String[]{"XM", "CYM", "XB", "MZ", "CSRQ", "ZZSSXQ", "ZZXZ", "SFZH", "XP","QFJG","YXQX"});
        String serviceId = "S10-00000298";
        RbspCall call = getServiceCall(serviceId);
        String result = call.invoke(params);//调用返回结果
        //System.out.println(result);
        //String result = getTestResult2();
        People people = getPeopleByXml(result);

        System.out.println(people);

        return people;
    }

    private RbspCall getServiceCall(String serviceId) {
        RbspService service = new RbspService("C37-06000053", serviceId);
        service.setUserCardId(userCardId);//设置用户身份编号
        service.setUserDept(userDept);//设置用户单位
        service.setUserName(userName);//设置用户名
        service.setPkiId("#");//设置PKI编号
        RbspCall call = service.createCall();//创建RbspCall
        call.setUrl(url);//设置总线地址
        call.setMethod(RbspConsts.METHOD_QUERY);//设置WebService接口方法

        return call;
    }



    private People getPeopleByXml(String xml) {
        if (null == xml) {
            return null;
        }
        People people = null;
        try {
            Document document = DocumentHelper.parseText(xml);
            Element rootElement = document.getRootElement();
            Element valueElement = rootElement.element("Method").element("Items").element("Item").element("Value");
            List<String> columnNameList = new LinkedList<String>();
            Iterator rowIt = valueElement.elementIterator("Row");
            int i = 0;
            while (rowIt.hasNext()) {
                Element rowElement = (Element) rowIt.next();
                if (i == 0) {
                    i++;
                    continue;
                } else if (i == 1) {
                    Iterator dataIt = rowElement.elementIterator("Data");
                    while (dataIt.hasNext()) {
                        Element dataElement = (Element) dataIt.next();
                        String columnName = dataElement.getText();
                        columnNameList.add(columnName);
                    }
                } else {
                    people = new People();

                    Iterator dataIt = rowElement.elementIterator("Data");
                    int columnIndex = 0;
                    while (dataIt.hasNext()) {
                        Element dataElement = (Element) dataIt.next();
                        String data = dataElement.getText();
                        String columnName = columnNameList.get(columnIndex);
                        if ("XM".equals(columnName)) {
                            people.setXm(data);
                        } else if ("CYM".equals(columnName)) {
                            people.setCym(data);
                        } else if ("CYM".equals(columnName)) {
                            people.setCym(data);
                        } else if ("XB".equals(columnName)) {
                            people.setXb(data);
                        } else if ("MZ".equals(columnName)) {
                            people.setMz(data);
                        } else if ("CSRQ".equals(columnName)) {
                            people.setCsrq(data);
                        } else if ("ZZSSXQ".equals(columnName)) {
                            people.setZzssxq(data);
                        } else if ("ZZXZ".equals(columnName)) {
                            people.setZzxz(data);
                        } else if ("SFZH".equals(columnName)) {
                            people.setSfzh(data);
                        } else if ("XP".equals(columnName)) {
                            people.setXp(data);
                        }


                        columnIndex++;
                    }

                }
                i++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return people;
    }


    private Timestamp parseTime(String input) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp timestamp = null;
        try {
            Date date = simpleDateFormat.parse(input);
            timestamp = new Timestamp(date.getTime());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return timestamp;
    }


}
